//
//  Tests.swift
//  Tests
//
//  Created by LL on 2023/4/6.
//

import XCTest

final class StringCategoriesTests: XCTestCase {

    func test_pathExtension() throws {
        XCTAssertEqual("/tmp/scratch.tiff".pathExtension, "tiff")
        XCTAssertEqual("/tmp/scratch.tiff ".pathExtension, "tiff")
        XCTAssertEqual("/tmp/scratch. tiff ".pathExtension, "tiff")
        XCTAssertEqual(".scratch.tiff".pathExtension, "tiff")
        XCTAssertEqual(".scratch.tiff/".pathExtension, "tiff")
        XCTAssertEqual("/tmp/scratch".pathExtension, "")
        XCTAssertEqual("/tmp/".pathExtension, "")
        XCTAssertEqual("/tmp/scratch..tiff".pathExtension, "tiff")
        XCTAssertEqual("/tmp/scratch.".pathExtension, "")
        XCTAssertEqual("/tmp/scratch.framework/FBSDKCoreKit".pathExtension, "")
        XCTAssertEqual(".tiff".pathExtension, "tiff")
        XCTAssertEqual("scratch".pathExtension, "")
        XCTAssertEqual("".pathExtension, "")
        XCTAssertEqual("  \n  \t".pathExtension, "")
    }
    
    
    func test_deletingPathExtension() throws {
        XCTAssertEqual("/tmp/scratch.tiff".deletingPathExtension, "/tmp/scratch")
        XCTAssertEqual("/tmp/scratch.tiff ".deletingPathExtension, "/tmp/scratch")
        XCTAssertEqual("/tmp/scratch. tiff ".deletingPathExtension, "/tmp/scratch")
        XCTAssertEqual(".scratch.tiff".deletingPathExtension, ".scratch")
        XCTAssertEqual(".scratch.tiff/".deletingPathExtension, ".scratch")
        XCTAssertEqual("/tmp/scratch".deletingPathExtension, "/tmp/scratch")
        XCTAssertEqual("/tmp/".deletingPathExtension, "/tmp/")
        XCTAssertEqual("/tmp/scratch..tiff".deletingPathExtension, "/tmp/scratch.")
        XCTAssertEqual("/tmp/scratch.".deletingPathExtension, "/tmp/scratch")
        XCTAssertEqual("/tmp/scratch.framework/FBSDKCoreKit".deletingPathExtension, "/tmp/scratch.framework/FBSDKCoreKit")
        XCTAssertEqual(".tiff".deletingPathExtension, "")
        XCTAssertEqual("scratch".deletingPathExtension, "scratch")
        XCTAssertEqual("".deletingPathExtension, "")
        XCTAssertEqual("  \n  \t".deletingPathExtension, "  \n  \t")
    }
    
    
    func test_lastPathComponent() throws {
        XCTAssertEqual("/tmp/scratch.tiff".lastPathComponent, "scratch.tiff")
        XCTAssertEqual("/tmp/scratch".lastPathComponent, "scratch")
        XCTAssertEqual("/tmp/".lastPathComponent, "tmp")
        XCTAssertEqual("scratch///".lastPathComponent, "scratch")
        XCTAssertEqual("/".lastPathComponent, "/")
        XCTAssertEqual("".lastPathComponent, "")
    }
    
    
    func test_deletingLastPathComponent() throws {
        XCTAssertEqual("/tmp/scratch.tiff".deletingLastPathComponent, "/tmp")
        XCTAssertEqual("/tmp/lock/".deletingLastPathComponent, "/tmp")
        XCTAssertEqual("/tmp/".deletingLastPathComponent, "/")
        XCTAssertEqual("/tmp".deletingLastPathComponent, "/")
        XCTAssertEqual("/".deletingLastPathComponent, "/")
        XCTAssertEqual("/tmp///".deletingLastPathComponent, "/")
        XCTAssertEqual("".deletingLastPathComponent, "")
    }
    
    
    func test_endCharIndex() throws {
        var string = "scratch"
        var index = string.endCharIndex
        XCTAssertEqual(String(string[index!]), "h")

        string = ""
        index = string.endCharIndex
        XCTAssertNil(index)
        
        string = "f"
        index = string.endCharIndex
        XCTAssertEqual(string[index!], "f")
    }
    
    
    func test_isFolder() throws {
        XCTAssertFalse("".isFolder)
        
        let bundlePath = Bundle.main.bundlePath
        XCTAssertTrue(bundlePath.isFolder)
        
        var path = bundlePath + "/XCTRunner.app"
        XCTAssertTrue(path.isFolder)
        
        path = bundlePath + "/123"
        XCTAssertFalse(path.isFolder)
        
        path = bundlePath + "/xctest"
        XCTAssertFalse(path.isFolder)
    }
    
    
    func test_isFile() throws {
        XCTAssertFalse("".isFile)
        
        let bundlePath = Bundle.main.bundlePath
        XCTAssertFalse(bundlePath.isFile)
        
        var path = bundlePath + "/xctest"
        XCTAssertTrue(path.isFile)
        
        path = bundlePath + "/123.txt"
        XCTAssertFalse(path.isFile)
        
        path = bundlePath + "/XCTRunner.app"
        XCTAssertFalse(path.isFile)
    }
    
    
    func test_isExist() throws {
        XCTAssertFalse("".isExist)
        
        let bundlePath = Bundle.main.bundlePath
        XCTAssertTrue(bundlePath.isExist)
        
        var path = bundlePath + "/xctest"
        XCTAssertTrue(path.isExist)
        
        path = bundlePath + "/123.txt"
        XCTAssertFalse(path.isExist)
        
        path = bundlePath + "/XCTRunner.app"
        XCTAssertTrue(path.isExist)
    }
    
    
    func test_appendingPathComponent() throws {
        XCTAssertEqual("/tmp".appendingPathComponent("", "scratch.tiff"), "/tmp/scratch.tiff")
        XCTAssertEqual("/tmp/".appendingPathComponent("abc", "scratch.tiff"), "/tmp/abc/scratch.tiff")
        XCTAssertEqual("/tmp///".appendingPathComponent("scratch.tiff"), "/tmp/scratch.tiff")
        XCTAssertEqual("/tmp///".appendingPathComponent("//scratch.tiff"), "/tmp/scratch.tiff")
        XCTAssertEqual("/".appendingPathComponent("scratch.tiff"), "/scratch.tiff")
        XCTAssertEqual("".appendingPathComponent("scratch.tiff"), "scratch.tiff")
    }
    
    
    func test_trimmingString() throws {
        XCTAssertEqual("/tmp/".trimmingString("/"), "tmp")
        XCTAssertEqual("tmp/".trimmingString("/"), "tmp")
        XCTAssertEqual("/t/m//p/".trimmingString("/"), "t/m//p")
        XCTAssertEqual("//tm//p//".trimmingString("/"), "tm//p")
        XCTAssertEqual("/".trimmingString("/"), "")
        XCTAssertEqual("@endtmp@end".trimmingString("@end"), "tmp")
        XCTAssertEqual("/tmp/".trimmingString(""), "/tmp/")
        XCTAssertEqual("".trimmingString("/"), "")
    }
    
    
    func test_trimmingPrefixString() throws {
        XCTAssertEqual("/tmp/".trimmingPrefixString("/"), "tmp/")
        XCTAssertEqual("///tmp/".trimmingPrefixString("/"), "tmp/")
        XCTAssertEqual("///tmp/".trimmingPrefixString("@"), "///tmp/")
        XCTAssertEqual("endendtmp/".trimmingPrefixString("end"), "tmp/")
        XCTAssertEqual("/tmp/".trimmingPrefixString(""), "/tmp/")
        XCTAssertEqual("".trimmingPrefixString("/"), "")
    }
    
    
    func test_trimmingSuffixString() throws {
        XCTAssertEqual("/tmp/".trimmingSuffixString("/"), "/tmp")
        XCTAssertEqual("/tmp///".trimmingSuffixString("/"), "/tmp")
        XCTAssertEqual("///tmp/".trimmingSuffixString("@"), "///tmp/")
        XCTAssertEqual("tmpendend".trimmingSuffixString("end"), "tmp")
        XCTAssertEqual("/tmp/".trimmingSuffixString(""), "/tmp/")
        XCTAssertEqual("".trimmingSuffixString("/"), "")
    }
    
    
    func test_lastIndex() throws {
        let content = "/Suf/fixStri/ng/"
        
        XCTAssertNil(content.lastIndex(of: "/", number: 0))
        XCTAssertNil(content.lastIndex(of: "/", number: -1))
        XCTAssertNil(content.lastIndex(of: "1", number: 1))
        XCTAssertEqual(content.lastIndex(of: "/", number: 1), content.endCharIndex)
        XCTAssertEqual(content.lastIndex(of: "/", number: 2), content.index(content.endIndex, offsetBy: -4))
        XCTAssertEqual(content.lastIndex(of: "/", number: 3), content.index(content.startIndex, offsetBy: 4))
        XCTAssertEqual(content.lastIndex(of: "/", number: 4), content.startIndex)
        XCTAssertNil(content.lastIndex(of: "/", number: 5))
    }
    
    
    func test_firstIndex() throws {
        let content = "/Suf/fixStri/ng/"
        
        XCTAssertNil(content.firstIndex(of: "/", number: 0))
        XCTAssertNil(content.firstIndex(of: "/", number: -1))
        XCTAssertNil(content.firstIndex(of: "1", number: 1))
        XCTAssertEqual(content.firstIndex(of: "/", number: 1), content.startIndex)
        XCTAssertEqual(content.firstIndex(of: "/", number: 2), content.index(content.startIndex, offsetBy: 4))
        XCTAssertEqual(content.firstIndex(of: "/", number: 3), content.index(content.endIndex, offsetBy: -4))
        XCTAssertEqual(content.firstIndex(of: "/", number: 4), content.endCharIndex)
        XCTAssertNil(content.firstIndex(of: "/", number: 5))
    }
}
